﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using System.Resources;
using WebCrawler.ProcessingFiles.CategoryClasses;

namespace WebCrawler
{
    public partial class ProcessOneProduct : System.Web.UI.Page
    {
        ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();
        NewEggClasses vendor = new NewEggClasses();

        Exception ex = new Exception();
        ProductDataDataContextDataContext dce = new ProductDataDataContextDataContext();
        public String errorPage = "ProcessOneProduct";


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                Response.Write("<p class='URLS'/>");
                Response.Write("<div class='products' style='display:block'>");
                GetProductPages();
                Response.Write("</div>");


            }
            if (Request.QueryString.Count > 0)
            {
                try
                {
                    string dataString = Request.QueryString["dataString"];
                    string product = "";
                    string prodname = "";
                    string prodvalue = "";
                    string workingStr = "";

                    dataString = dataString.Replace(",", " # ");

                    char[] prodnum = new char[] { '%' };
                    string[] prodNum = dataString.Split(prodnum);
                    //foreach (var c in prodNum)
                    //{
                    product = prodNum[0].ToString();
                    dataString = prodNum[1].ToString();
                    //}

                    char[] separator = new char[] { '^' };
                    char[] TS = new char[] { '[' };
                    char[] TE = new char[] { ']' };
                    char[] S = new char[] { '~' };
                    char[] IS = new char[] { '{' };
                    char[] IE = new char[] { '}' };

                    dataString = dataString.TrimEnd(separator);
                    dataString = dataString.Replace("#", ",");
                    dataString = dataString.Replace("|nbsp;", "  ");

                    dataString = dataString.Replace("|gt;", "  ");
                    dataString = dataString.Replace("|", "  ");




                    string[] strSplitArr = dataString.Split(separator);
                    foreach (var c in strSplitArr)
                    {

                        workingStr = c;
                        workingStr = workingStr.TrimStart(TS);
                        workingStr = workingStr.TrimEnd(TE);

                        string[] itemList = workingStr.Split(S);

                        itemList[0] = itemList[0].TrimStart(IS).TrimEnd(IE);
                        itemList[1] = itemList[1].TrimStart(IS).TrimEnd(IE);

                        prodname = itemList[0].ToString().Trim();
                        prodvalue = itemList[1].ToString().Trim();




                        string Name = Server.HtmlEncode(prodname).Trim();
                        string Value = Server.HtmlEncode(prodvalue).Trim();
                        int ProductID = 0;
                        if (!string.IsNullOrEmpty(product) && Int32.TryParse(product, out ProductID))
                        {
                            ProductID = int.Parse(product);
                        }


                        if (Value.Length > 0)
                        {
                            ProcessProductData(ProductID, Name, Value);
                        }
                    }
                }
                catch (Exception ex)
                {


                    ProcessErrorText(ex);

                }
            }
        }


        protected void ProcessProductData(int productID, string prodname, string prodvalue)
        {
            try
            {

               var query = from pda in dc.ProductDatas
                            where pda.productDataID == productID && pda.productDataName.Trim() == prodname.Trim() && pda.productDataValue.Trim() == prodvalue.Trim()
                            select pda;

                if (query.Count() == 0)
                {

                    ProductData pd = new ProductData();
                    pd.productLinksID = productID;
                    pd.productDataName = prodname;
                    pd.productDataValue = prodvalue;

                    dc.ProductDatas.InsertOnSubmit(pd);
                    dc.SubmitChanges();
                }

                var qry = from pl in dc.ProductLinks where pl.productID == productID select pl;

                var item = qry.Single();

                item.process = 1;
                dc.SubmitChanges();

                UpdateProductLink(productID);
            }
            catch (Exception ex)
            {


                ProcessErrorText(ex);

            }

        }

        protected void UpdateProductLink(int productID)
        {

            var query = (from p in dc.ProductDatas where p.productLinksID == productID && p.productDataName == "Name" select p.productDataValue).FirstOrDefault();
            if (query != null)
            {

                var q = (from pl in dc.ProductLinks where pl.productID == productID select pl).FirstOrDefault();

                if (q != null)
                {
                    q.productName = query;
                }
            }

            var query2 = from p in dc.ProductDatas where p.productLinksID == productID && p.productDataName.Contains("Cat") select p;
            if (query2 != null)
            {

                foreach (var item in query2)
                {
                    var q2 = (from c in dc.Categories where c.catName == item.productDataValue && !item.productDataValue.Contains("Item") select c).FirstOrDefault();

                    if(q2 != null)
                    {

                        int catID = q2.categoryID;

                        var q3 = (from pl in dc.ProductLinks where pl.productID == productID select pl).FirstOrDefault();

                        if (q3 != null)
                        {
                            q3.catID = catID;
                        }
                    }
                }
            }
            
        
        }


        protected void GetProductPages()
        {
            try
            {
                var query = (from pd in dc.PredictProducts where pd.predictResult == 1 select pd).FirstOrDefault();

                if (query != null)
                {
                    //query.predictResult = 1;
                    //dc.SubmitChanges();

                    var qry = from pl in dc.ProductLinks where pl.productURL == query.predictURL select pl;

                    if (qry.Count() == 0)
                    {

                        ProductLink pl = new ProductLink();
                        pl.productName = "Predict";
                        pl.productURL = query.predictURL;
                        pl.catID = 0;
                        pl.process = 0;
                        pl.vendorID = 1;

                        dc.ProductLinks.InsertOnSubmit(pl);
                        dc.SubmitChanges();

                        var q = (from pl1 in dc.ProductLinks where pl1.productURL == query.predictURL select pl1).FirstOrDefault();

                        string strResult = "";
                        string CurrURL = query.predictURL.Replace("|", "&").Trim();
                        string productID = q.productID.ToString();

                        WebResponse objResponse;
                        WebRequest objRequest = System.Net.HttpWebRequest.Create(CurrURL);

                        objResponse = objRequest.GetResponse();

                        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
                        {
                            strResult = sr.ReadToEnd();
                            sr.Close();
                        }

                        strResult = strResult.Replace("\"", "");
                        strResult = strResult.Replace("#", "");
                        strResult = strResult.Replace("&", "|");
                        strResult = strResult.Remove(0, 10000);



                        Response.Write("<p class=CURRURL>");
                        Response.Write(productID);
                        Response.Write("</p>");
                        Response.Write(strResult);

                       
                    }
                    else
                    {
                        var qry2 = from pd in dc.PredictProducts where pd.predictURL == query.predictURL && pd.predictResult == 1 select pd;

                        foreach (var item in qry2)
                        {
                            item.predictResult = 2;
                            dc.SubmitChanges();
                        }

                        GetNextPage(null, null);
                    }
                }
                
            }
            catch (Exception ex)
            {


                ProcessErrorText(ex);

            }

        }

        protected void ProcessErrorText(Exception ex)
        {

            StringBuilder errorText = new StringBuilder();

            if (ex != null)
            {
                if (ex.Source != null)
                {
                    errorText.Append(errorPage + " :source: " + ex.Source);
                }
                if (ex.Message != null)
                {
                    errorText.Append(errorPage + " :message: " + ex.Message);
                }
                if (ex.InnerException != null)
                {
                    errorText.Append(errorPage + " :innerexception: " + ex.InnerException);
                }
                if (ex.TargetSite != null)
                {
                    errorText.Append(errorPage + " :target: " + ex.TargetSite);
                }

            }
            else
            {
                errorText.Append("unknown error");
            }

            ProductDataDataContextDataContext dce = new ProductDataDataContextDataContext();

            WebCrawler wc = new WebCrawler();
            wc.webCrawlerErrorText = errorText.ToString();

            dce.WebCrawlers.InsertOnSubmit(wc);
            dce.SubmitChanges();
        }

        protected void GetNextPage(object sender, EventArgs e)
        {
            Response.Redirect("/ProcessingFiles/RedirectForm.aspx?Type=2&Name=Predict");
        }
    }
}